﻿using Sidvall.IO;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Sidvall.Tasks
{
    public sealed class CopyFileTask : MonitoredTaskBase, System.IProgress<Sidvall.IO.FileProgress>
    {
        #region Public Members

        #region ExecuteAsync

        public override async Task ExecuteAsync(IProgress<MonitoredTaskProgress> progress, CancellationToken cancellationToken)
        {
            this.Progress = progress;
            await StorageGroupManager.CopyFileAsync(this.SourceStorageManager, this.SourcePath, this.DestinationStorageManager, this.DestinationPath, this, cancellationToken).ConfigureAwait(false);
        }

        #endregion
        #region IProgress members

        void System.IProgress<Sidvall.IO.FileProgress>.Report(Sidvall.IO.FileProgress value)
        {
            if (value == null)
                return;
            var context = new Sidvall.Tasks.MonitoredTaskProgress()
            {
                TaskId = this.TaskId,
                Description = GetDescription(),
                TotalWork = value.Length,
                CompletedWork = value.Position,
            };
            this.Progress.Report(context);
        }

        #endregion

        #endregion
        #region Private Members

        private System.IProgress<Sidvall.Tasks.MonitoredTaskProgress> Progress { get; set; }
        private StorageManager SourceStorageManager { get; set; }
        private StorageManager DestinationStorageManager { get; set; }
        private string SourcePath { get; set; }
        private string DestinationPath { get; set; }
        private string MessageFormat { get; set; }

        #region GetDescription

        private string GetDescription()
        {
            string value;

            if (!string.IsNullOrWhiteSpace(this.MessageFormat))
            {
                value = this.MessageFormat.Replace("{SourcePath}", this.SourcePath).Replace("{DestinationPath}", this.DestinationPath);
                value = value.Replace("{SourceName}", System.IO.Path.GetFileName(this.SourcePath)).Replace("{DestinationName}", System.IO.Path.GetFileName(this.DestinationPath));
                return value;
            }
            return "Copying " + this.SourcePath + " to " + this.DestinationPath;
        }

        #endregion

        #endregion
        #region Constructor

        public CopyFileTask(StorageManager sourceStorageManager, string sourcePath, StorageManager destinationStorageManager, string destinationPath)
        {
            this.SourceStorageManager = sourceStorageManager;
            this.SourcePath = sourcePath;
            this.DestinationStorageManager = destinationStorageManager;
            this.DestinationPath = destinationPath;
        }
        public CopyFileTask(StorageManager sourceStorageManager, string sourcePath, StorageManager destinationStorageManager, string destinationPath, string messageFormat)
            : this(sourceStorageManager, sourcePath, destinationStorageManager, destinationPath)
        {
            this.MessageFormat = messageFormat;
        }
        public CopyFileTask(StorageManager sourceStorageManager, string sourcePath, StorageManager destinationStorageManager, string destinationPath, string messageFormat, string taskId)
            : this(sourceStorageManager, sourcePath, destinationStorageManager, destinationPath, messageFormat)
        {
            this.TaskId = taskId;
        }

        #endregion
    }
}
